import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog } 	from 'vant';
Vue.use(Toast,Dialog);

export default {
	name: 'redHistory',
	data(){
		return {
			ajax:false,			//接口是否已返回数据
			pageState:1,		//1:购买订单   2:出售订单
			loading:false,
			pageIndex:1,
			list1:[],
			list2:[],
			finished:false,
			loadError:false,
			offset:0,
			userName:'',
			iconShown:false,
			coinsList:[
				{id:1,name:'DC',select:true,color:'#0080FF'},
				{id:2,name:'BTC',select:false,color:''},
				{id:3,name:'USDT',select:false,color:''}
			],
			last_id1:'',
			last_id:'',
			total1:0,
			total2:0,
			head_img:'',
			count:0,			//抢到的红包
			max_nums:0,			//手气最佳
		}
	},
	computed:{
		list(){
			return this.pageState===1?this.list1:this.list2
		},
		currentId(){
			return this.coinsList.find(item => item.select).name
		},
	},
	watch:{
		currentId(newVal){
			if(newVal === 'DC'){
				this.pageState = 1;
			}
		}
	},
	methods:{
		changeState(state){
			this.pageIndex = 1;
			this.list1 = [];
			this.list2 = [];
			this.pageState = state;
			this.finished = false;
			this.last_id = '';
			this.last_id1 = '';
			this.total1 = 0;
			this.total2 = 0;
			this.loading = true;
			this.init();
		},
		selectIcon(ele){
			//选择币种
			this.coinsList.map(item => {
				item.select = item.id === ele.id?true:false;
				item.color = item.id === ele.id?'#0080FF':'';
			})
			this.changeState(this.pageState)
			this.iconShown = false;
		},
		goDetail(item){
			// console.log(item)
			const getParams = {
            	nickname: this.pageIndex==1?item.name:this.userName,
            	url: '',
            	btc: (typeof item.amt != 'number' || this.currentId==='DC')?item.amt:item.amt.toFixed(this.currentId=='BTC'?8:2),
            	// btc: typeof item.amt === 'number'?item.amt.toFixed(this.currentId=='BTC'?8:2):item.amt,
            	coinType: this.currentId,
            	bsflag : ''
            };
            this.$router.push({
				path:'/redetails',
				query:{
					rid:item.rid,
					type:this.pageIndex==1?item.type:'',
					info:encodeURIComponent(JSON.stringify(getParams)),
					title:'',
				}
			})
		},
		init(){
			if(this.finished) return
			this.loading = true;
			let self = this;
			setTimeout(()=>{
				if(self.pageState === 1){
					self.dropList()
				}else{
					self.sendList();
				}
			},500)
			
		},
		sendList(){
			//发出的红包记录
			let params = {
				currency_id:this.coinsList.find(item => item.select).name
			}
			if(this.last_id){
				params.last_id = this.last_id;
			}
			util.fetch2(`${util.ajaxUrl}/red-packet/send-list`,params).then(res => {
				this.ajax = true;
				this.loading = false;
				Toast.clear();
				if(res.retCode === 0){
					this.total2 = res.retData.total;
					this.userName = res.retData.name;
					this.head_img = res.retData.head_img;
					this.last_id = res.retData.last_id;
					this.count = res.retData.count;
					this.list2 = this.list2.concat(res.retData.list);
					if(this.list.length == this.count || res.retData.list.length === 0) this.finished = true;
				}else{
					this.finished = true;
					Toast(res.retMsg || res.retHtml || this.$t('noError'))
				}
			}).catch(error => {
				this.finished = true;
				console.log(error)
			})
		},
		dropList(){
			//收到的红包记录
			let params = {
				currency_id:this.coinsList.find(item => item.select).name
			}
			if(this.last_id1){
				params.last_id = this.last_id1;
			}
			util.fetch2(`${util.ajaxUrl}/red-packet/user-packet-list`,params).then(res => {
				this.ajax = true;
				this.loading = false;
				Toast.clear();
				if(res.retCode === 0){
					this.total1 = res.retData.total;
					this.userName = res.retData.name;
					this.head_img = res.retData.head_img;
					this.count = res.retData.count;
					this.last_id1 = res.retData.last_id;
					this.max_nums = res.retData.max_nums;
					this.list1 = this.list1.concat(res.retData.list);
					if(this.list.length == this.count || res.retData.list.length === 0) this.finished = true;
				}else{
					this.finished = true;
					Toast(res.retMsg || res.retHtml || this.$t('noError'))
				}
			}).catch(error => {
				this.finished = true;
				console.log(error)
			})
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		Toast.loading({
			message: this.$t('loading')+'',
			duration:0,
			forbidClick: true
		});
		util.waitSDKReady().then(token => {
			this.token = token;
			if(this.isNi){
				this.coinsList = [{id:3,name:'USDT',select:true,color:'#0080FF'}];
			}
			// this.init();
		}).catch((e) => {
			Toast(this.$t('getTokenFail'))
		})
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				// if(this.token) this.init();
				util.callHandler('setStatusBarStyle',{
					style:'light'
				})
			})
			util.callHandler('setStatusBarStyle',{
				style:'light'
			})
		},300)
    }
}